﻿using System;
using System.Linq;
using System.Web;
using Data.Entity;
using System.Web.Security;
using Service;

namespace ECommerce
{
	public class SiteContext
	{
		#region fields

		private HttpContext _context = HttpContext.Current;
		//private UserExtension _userExt;
		private Language _WorkingLanguage;
		private ILanguageService _LanguageService;
		private IUserExtensionService _UserExtensionService;
		private const Double COOKIEEXPIRES = 128;
		
		public const string COOKIE_KEY_LANGUAGECULTURE = "Godbuying.LanguageCulture";

		#endregion

		#region properties

		public Language WorkingLanguage
		{
			get
			{
				if (_WorkingLanguage != null)
				{
					return _WorkingLanguage;
				}

				if (_LanguageService == null)
				{
					_LanguageService = new LanguageService();
				}

				if (User != null && UserExt != null)
				{
					var cultureCookie = CommonHelper.GetCookieString(SiteContext.COOKIE_KEY_LANGUAGECULTURE, false);

					if (string.IsNullOrEmpty(cultureCookie))
					{
						var languages = _LanguageService.GetAllLanguage().Where(exp => exp.LanguageId == UserExt.LanguageID);

						if (languages != null && languages.Count() > 0)
						{
							_WorkingLanguage = languages.First();
						}
					}
					else
					{
						var languages = _LanguageService.GetAllLanguage().Where(exp => exp.LanguageCulture.Equals(cultureCookie));

						if (languages != null && languages.Count() > 0)
						{
							_WorkingLanguage = languages.First();
						}
					}
				}
				else
				{
					var cultureCookie = CommonHelper.GetCookieString(SiteContext.COOKIE_KEY_LANGUAGECULTURE, false);
					var culture = System.Globalization.CultureInfo.CurrentCulture.Name;
					if (!string.IsNullOrEmpty(cultureCookie))
					{
						culture = cultureCookie;
					}

					var languages = _LanguageService.GetAllLanguage().Where(exp => exp.LanguageCulture.Equals(culture));

					if (languages != null && languages.Count() > 0)
					{
						_WorkingLanguage = languages.First();
					}
				}

				return _WorkingLanguage;
			}
			set
			{
				_WorkingLanguage = value;
			}
		}

		public MembershipUser User
		{
			get
			{
				if (_context == null)
				{
					return null;
				}

				if (_context.User == null || _context.User.Identity == null || !_context.User.Identity.IsAuthenticated)
				{
					return null;
				}

				return Membership.GetUser(_context.User.Identity.Name);
			}
		}

		public UserExtension UserExt
		{
			get
			{
				if (User == null)
				{
					return null;
				}

				if (_UserExtensionService == null)
				{
					_UserExtensionService = new Service.UserExtensionService();
				}

				var userID = User.ProviderUserKey as Guid?;

				if (userID.HasValue)
				{
					return _UserExtensionService.GetUserExtensionByUserID(userID.Value);
				}

				return null;
			}
		}

		public System.Web.Caching.Cache SiteCache
		{
			get
			{
				return _context.Cache;
			}
		}

		public string IPAddress
		{
			get
			{
				if (_context != null &&
					_context.Request != null &&
					_context.Request.UserHostAddress != null)
					return _context.Request.UserHostAddress;
				else
					return string.Empty;
			}
		}

		public static SiteContext Current
		{
			get
			{
				if (HttpContext.Current == null)
				{
					return null;
				}

				if (HttpContext.Current.Items["SiteContext"] == null)
				{
					SiteContext context = new SiteContext();
					HttpContext.Current.Items.Add("SiteContext", context);

					return context;
				}

				return (SiteContext)HttpContext.Current.Items["SiteContext"];
			}
		}

		#endregion

		#region constructor

		private SiteContext() { }

		#endregion

		#region static methods


		#endregion

		#region instance methods

		public void SetCookie(string key, string val)
		{
			if (string.IsNullOrEmpty(key))
			{
				return;
			}

			HttpCookie cookie = new HttpCookie(key);
			cookie.Value = val;

			if (string.IsNullOrEmpty(val))
			{
				cookie.Expires = DateTime.Now.AddMonths(-1);
			}
			else
			{
				cookie.Expires = DateTime.Now.AddHours(SiteContext.COOKIEEXPIRES);
			}

			_context.Response.Cookies.Remove(key);
			_context.Response.Cookies.Add(cookie);
		}

		#endregion
	}
}
